package com.xuecheng.manage_cms.service;

import com.sun.org.apache.bcel.internal.generic.IFNONNULL;
import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.request.QueryPageRequest;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.manage_cms.dao.CmsPageRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;

/**
 * @program: xcservice001
 * @description: cms业务处理
 * @className: CmsService
 * @author: rjt99999
 * @version: 1.0.0
 * @create: 2020-09-19 15:36
 **/
@Service
public class CmsService {

    @Autowired
    CmsPageRepository cmsPageRepository;

    //分页查询

    /**
     * @param page 页码 从一开始
     * @param size 每页的记录数
     * @param queryPageRequest 返回的数据
     * @return QueryResponseResult
     */
    //分页查询
    public QueryResponseResult findList(int page, int size, QueryPageRequest queryPageRequest) {

        //条件查询,模糊
        if (queryPageRequest==null){
            queryPageRequest = new QueryPageRequest();
        }
        //条件值对象
        CmsPage cmsPage = new CmsPage();
        //条件匹配器
        ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("pageAliase",
                ExampleMatcher.GenericPropertyMatchers.contains());

        //站点ip
        if (StringUtils.isEmpty(queryPageRequest.getSiteId())){
            cmsPage.setSiteId(queryPageRequest.getSiteId());

        }
        //模板id
        if (StringUtils.isEmpty(queryPageRequest.getTemplateId())){
         cmsPage.setTemplateId(queryPageRequest.getTemplateId());

        }
        //设置页面别名作为查询条件
        if (StringUtils.isEmpty(queryPageRequest.getPageAliase())){
            cmsPage.setPageAliase(queryPageRequest.getPageAliase());

        }

        //定义条件对象Example
        Example<CmsPage> cmsPageExample = Example.of(cmsPage,exampleMatcher);










        //分页查询
        //第一页显示
        if (page <= 0) {
            page = 1;
        }
        //数据库从0查询
        page = page -1;

        if (size <= 0) {
            size = 10;//默认
        }
        //分页方法
        Pageable pageable = PageRequest.of(page,size);

        Page<CmsPage> all = cmsPageRepository.findAll(cmsPageExample,pageable);
        QueryResult queryResult = new QueryResult();

        queryResult.setList(all.getContent());//数据列表
        queryResult.setTotal(all.getTotalElements());//总记录数

        QueryResponseResult queryResponseResult = new QueryResponseResult(CommonCode.SUCCESS,queryResult);







        return queryResponseResult;













    }


}
